import { MajongsHoldsConfig } from "../Libs/Config/Config";

export class MjEffect {
    //出牌以后的动画
    public static moveHitToPos(holdNode:cc.Node,mjs:number[],hitMj:number,lastMj:number,callBack:Function):void{
        //得到打出去的牌的索引
        let arrIdx = mjs.indexOf(hitMj)
        mjs.splice(arrIdx,1)
        let config = MajongsHoldsConfig.Holds[0]    
        let hitMjs = -1
        for(let i = 0; i < holdNode.childrenCount;i++){
            if(holdNode.children[i].tag == hitMj){
                holdNode.children[i].tag = -1
                holdNode.children[i].active = false
                hitMjs = i
                break
            }
        }
        let tmpMjs = mjs.slice()
        tmpMjs.concat(lastMj)
        tmpMjs = tmpMjs.sort(function(a,b){return a - b})  

        let lastMjIndex = holdNode.children[13].tag
        let curIndex = tmpMjs.indexOf(lastMjIndex)
        //从我当前的位置开始移动
        if(curIndex > hitMjs){
            for(let i = hitMjs; i < curIndex;i++){TweenLite.to(holdNode.children[i + 1],0.2,{x:config.pos.x + i * config.offset.x})}
        }else{
            for(let i = curIndex; i < hitMjs;i++){TweenLite.to(holdNode.children[i],0.2,{x:config.pos.x + (i + 1) * config.offset.x})}
        }

        if(hitMjs == 13){callBack(tmpMjs);return}

        //得到要移动到的位置,
        let moveToHolds : cc.Vec2 = holdNode.children[curIndex].getPosition()
        if(curIndex == tmpMjs.length - 1){
            TweenLite.to(holdNode.children[13],0.2,{x:moveToHolds.x,onComplete:()=>{
                callBack(tmpMjs)
            }})
        }else{
            let timeline:TimelineLite = new TimelineLite;
            timeline.add(TweenLite.to(holdNode.children[13],0.2,{x:moveToHolds.x,y:150,rotation:75}))
            timeline.add(TweenLite.to(holdNode.children[13],0.1,{rotation:0}))
            timeline.add(TweenLite.to(holdNode.children[13],0.1,{y:0,onComplete:()=>{
                console.log("动作执行完毕",tmpMjs)
                callBack(tmpMjs)
            }}))
        }
    }

}